/****************************************************************************
 *   This file is part of Q2D.                                              *
 *                                                                          *
 *   Q2D is free software: you can redistribute it and/or modify            *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation, either version 3 of the License, or      *
 *   (at your option) any later version.                                    *
 *                                                                          *
 *   Q2D is distributed in the hope that it will be useful,                 *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for more details.                           *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with Q2D.  If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                          *
 ****************************************************************************/

#include "merchant.h"

Merchant::Merchant(): QObject()
{
       this->setName("Merchant");
}

void Merchant::setName(QString name)
{
        this->name = name;
}

QString Merchant::getName()
{
        return this->name;
}

bool Merchant::isBuyingAuthorised()
{
        return this->buyingAuthorised;
}

void Merchant::setBuyingAuthorised(bool auth)
{
        this->buyingAuthorised = auth;
}

bool Merchant::isSellingAuthorised()
{
        return this->sellingAuthorised;
}

void Merchant::setSellingAuthorised(bool auth)
{
        this->sellingAuthorised = auth;
}

bool Merchant::weaponsAvailable()
{
        if(this->weaponList.empty())
                return false;
        else
                return true;
}

uint Merchant::moneyRemaining()
{
        return money;
}

int Merchant::weaponIndex(Weapon &weapon)
{
        int i;
        QPair<Weapon, QPair<int, qreal> > *current;


        for(i=0;i<this->weaponList.size();i++){
                current = &weaponList[i];
                if(current->first == weapon){
                        return i;
                }
        }

        return -1;
}

bool Merchant::add(Weapon weapon, int number, qreal price)
{
      bool ok;
      QVariant var;

      var.setValue(weapon);
      ok = notifyMerchant(WEAPON_ADDED, &var, number, price);

      if(ok){
              int index = this->weaponIndex(weapon);

              if(index != -1){
                        this->weaponList[index].second.first += number;
              }else{
                      QPair<Weapon, QPair<int, qreal> > item;
                      item.second.first = number;
                      if(price != -1){
                        item.second.second = price;
                      }else{
                              item.second.second = weapon.getPrice();
                      }

                      this->weaponList.append(item);
              }
      }else{
              return false;
      }

      emit itemAdded();
      return true;
}

int Merchant::numberAvailable(Weapon weapon)
{
        int index;
        index = this->weaponIndex(weapon);

        if(index != -1){
                return weaponList[index].second.first;
        }else{
                return 0;
        }
}

bool Merchant::changePrice(Weapon weapon, qreal price)
{
        bool ok;
        ok = notifyMerchant(WEAPON_PRICE, NULL, -1, price);

        if(!ok){
                return false;
        }else{
                this->weaponList[this->weaponIndex(weapon)].second.second = price;
                return true;
        }
}

bool Merchant::remove(Weapon weapon, int number)
{
      bool ok;
      QVariant var;

      var.setValue(weapon);
      ok = notifyMerchant(WEAPON_REMOVED, &var, number);

      if(ok){
              int index = this->weaponIndex(weapon);

              if(index != -1){
                        if((number == -1) || (weaponList[index].second.first == number)){
                                this->weaponList.removeAt(index);
                        }else{
                                weaponList[index].second.first -= number;
                        }
              }else{
                      return false;
              }
      }else{
              return false;
      }

      emit itemRemoved();
      return true;
}

bool Merchant::buy(Weapon weapon, int number, qreal price)
{
      bool ok;
      QVariant var;
      qreal total;

      if(!isBuyingAuthorised())
              return false;

      if(number > this->numberAvailable(weapon)){
              return false;
      }

      var.setValue(weapon);
      ok = notifyMerchant(WEAPON_BOUGHT, &var, number, price);

      
      if(ok){
              int index = this->weaponIndex(weapon);

              if(index != -1){
                        this->weaponList[index].second.first -= number;

                        if(price != -1){
                                total = number * price;
                        }else{
                                total = number * this->weaponList[index].second.second;
                        }
                        this->money += total;
              }else{
                      return false;
              }
      }else{
              return false;
      }

      emit itemBought();
      return true;
}

bool Merchant::sell(Weapon weapon, int number, qreal price)
{
      bool ok;
      QVariant var;
      qreal total;
      int index;

      if(!isBuyingAuthorised())
              return false;

      index = this->weaponIndex(weapon);
      if(price == -1){
              total = number * (weaponList[index].second.second / 2.0);
      }else{
              total = number * price;
      }

      if(total > this->money){
              return false;
      }

      var.setValue(weapon);
      ok = notifyMerchant(WEAPON_SOLD, &var, number, price);

      if(ok){
              this->money -= total;

              if(index != -1){
                        this->weaponList[index].second.first -= number;

                        if(price != -1){
                                total = number * price;
                        }else{
                                total = number * this->weaponList[index].second.second;
                        }

              }else{
                      return false;
              }
      }else{
              return false;
      }

      emit itemSold();
      return true;
}

//bool Merchant::armorsAvailable()
//{
//        if(this->armorList.empty())
//                return false;
//        else
//                return true;
//}
//
//bool Merchant::goodsAvailable()
//{
//        if(this->goodList.empty())
//                return false;
//        else
//                return true;
//}

bool Merchant::notifyMerchant(merchantAction action, QVariant * item, int number, qreal price)
{
        return true;
}
